package com.voute.Presenter.Impl;

import com.voute.Consts.Constants;
import com.voute.Model.Bean.BaseBean;
import com.voute.Model.Bean.LoginInfoBean;
import com.voute.Model.Bean.UxTagBean;
import com.voute.Model.ILoginModel;
import com.voute.Model.IUxTagModel;
import com.voute.Model.Impl.LoginModelImpl;
import com.voute.Model.Impl.UXTagModelImpl;
import com.voute.Presenter.ILoginPresenter;
import com.voute.Utils.CommonSubscriber;
import com.voute.Utils.RSAUtils;
import com.voute.Utils.TransformUtils;
import com.voute.View.IView.ILoginView;

import java.util.HashMap;

import rx.Observable;
import rx.functions.Action0;
import rx.functions.Func1;

/**
 * Created by liucxu on 2017/1/17.
 */

public class LoginPresenterImpl implements ILoginPresenter {
    private ILoginView login_view;
    private ILoginModel model;
    private IUxTagModel tagModel;

    public LoginPresenterImpl(ILoginView view) {
        login_view = view;
        model=new LoginModelImpl();
        tagModel=new UXTagModelImpl();
    }

    @Override
    public void login(final String phone, final String password) {
        HashMap<String,String> values=new HashMap<String, String>();
        values.put("phone",phone);
        String encrypiedPassword= RSAUtils.encrypyByPublicKey(password.getBytes(), Constants.getRsaKey());
        values.put("password", encrypiedPassword);
        model.login(values).compose(TransformUtils.<LoginInfoBean>defaultSchedulers()).doOnSubscribe(new Action0() {
            @Override
            public void call() {
                login_view.showLoading();
            }
        }).subscribe(new CommonSubscriber<LoginInfoBean>() {
            @Override
            public void onSuccess(LoginInfoBean response) {
                login_view.hideLoading();
                login_view.loginSuccess(response);
            }

            @Override
            public void onFailure(BaseBean response) {
                login_view.hideLoading();
                login_view.showError(response.getMsg());
            }
        });
    }
}
